package com.csu.letaobackend.controller;

import com.csu.letaobackend.bean.Item;
import com.csu.letaobackend.bean.Seller;
import com.csu.letaobackend.bean.Shop;
import com.csu.letaobackend.commom.CommonResponse;
import com.csu.letaobackend.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping("/shop")
public class ShopController {

      @Autowired
      ShopService shopService;

      @GetMapping("/shops")
      @ResponseBody
      public CommonResponse<List<Shop>> getShopsBySeller(HttpSession httpSession){
            Seller login_seller= (Seller) httpSession.getAttribute("login_seller");
            return shopService.getShopsBySeller(login_seller.getUsername());
      }

      @GetMapping("/shops/{shopid}")
      @ResponseBody
      public CommonResponse<Shop> getShops(@PathVariable int shopid,HttpSession httpSession){
            return shopService.getShops(shopid);
      }

      @PostMapping("/shops")
      @ResponseBody
      public CommonResponse<List<Shop>> addShop(Shop shop, HttpSession httpSession){
            Seller login_seller= (Seller) httpSession.getAttribute("login_seller");
            shop.setSellername(login_seller.getUsername());
            return shopService.addShop(shop);
      }

      @PutMapping("/shops")
      @ResponseBody
      public CommonResponse<Shop> updateShop(Shop shop, HttpSession httpSession){
            Seller login_seller= (Seller) httpSession.getAttribute("login_seller");
            shop.setSellername(login_seller.getUsername());
            return shopService.updateShop(shop);
      }

      @PostMapping("/shops/items")
      @ResponseBody
      public CommonResponse<String> addItem(Item item, HttpSession httpSession){
            return shopService.addItem(item);
      }


}
